package com.jshoperxms.controller.mall.backstage.goodstype;


import com.jshoperxms.Response;
import com.jshoperxms.controller.mall.backstage.base.BaseTController;
import com.jshoperxms.controller.mall.backstage.base.IBaseCtrl;
import com.jshoperxms.controller.mall.backstage.base.SearchModel;
import com.jshoperxms.controller.utils.BaseTools;
import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.statickey.SerialKeys;
import com.jshoperxms.controller.utils.statickey.StaticKey;
import com.jshoperxms.controller.utils.statickey.TagWords;
import com.jshoperxms.controller.utils.statickey.WarnWrods;
import com.jshoperxms.entity.GoodsTypeT;
import com.jshoperxms.entity.StoreInfoT;
import com.jshoperxms.service.GoodsTypeTService;
import com.jshoperxms.service.impl.Serial;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.*;


@Controller
@RequestMapping("/mall/goodstype")
public class GoodsTypeTController extends BaseTController implements IBaseCtrl<GoodsTypeT>{
	@Resource
	private GoodsTypeTService goodsTypeTService;

	//数据形式
	private Map<String, Object> responsedata = new HashMap<>();


	/**
	 *
	 * @api {post} /mall/goodstype/save 保存商品类型
	 * @apiName 保存商品类型
	 * @apiGroup  GoodsService
	 * @apiDescription 后台管理员添加商品类型
	 * @apiVersion 0.0.1
	 * @apiParam {String} name 商品类型名称
	 * @apiParam {String} goodsParameter 商品类型参数
	 * @apiParam {String} status 记录状态 启用或者禁用
	 *
	 * @apiSuccess {String} sucflag true标记
	 * @apiSuccess {String} msg  提示信息
	 *
	 * @apiUse operationError
	 * @apiUse operationSuccess
	 */
	@RequestMapping(value="/save",method= RequestMethod.POST)
	@Override
	public ResponseEntity save(@RequestBody GoodsTypeT rq) {
		if(StringUtils.isNotBlank(rq.getName())){
			if(checkDuplicates(rq.getName())){
				GoodsTypeT bean=new GoodsTypeT();
				bean.setGoodstypeid(this.serial.Serialid(SerialKeys.GOODSTYPE));
				bean.setName(StringUtils.trim(rq.getName()));
				bean.setGoodsParameter(rq.getGoodsParameter());
				bean.setCreatetime(DateTools.getSystemTimeBi());
				bean.setCreatorid(BaseTools.getAdminCreateId());
				bean.setStatus(rq.getStatus());
				bean.setUpdatetime(DateTools.getSystemTimeBi());
				bean.setVersiont(1);
				this.goodsTypeTService.save(bean);
				responsedata.put(TagWords.SUCCESS, true);
				responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
			}else{
				responsedata.put(TagWords.SUCCESS,false);
				responsedata.put(TagWords.MSG,WarnWrods.ERROR_HAVE_KEY);
			}
		}else{
			responsedata.put(TagWords.SUCCESS,false);
			responsedata.put(TagWords.MSG,WarnWrods.ERROR_PARAMS);
		}
		return Response.success(responsedata);
	}


	/**
	 *
	 * @api {post} /mall/goodstype/find 查询一条商品类型
	 * @apiName 查询一条的商品类型
	 * @apiGroup  GoodsService
	 * @apiDescription 查询一条商品类型
	 * @apiVersion 0.0.1
	 * @apiSuccess {String} sucflag true标记
	 * @apiSuccess {String} msg  提示信息
	 * @apiSuccess {GoodsTypeT} bean 数据对象
	 *
	 * @apiUse operationError
	 * @apiUse operationSuccess
	 */
	@RequestMapping(value="/find",method= RequestMethod.POST)
	@Override
	public ResponseEntity find(@RequestBody GoodsTypeT rq) {
		if(StringUtils.isBlank(rq.getGoodstypeid())){
			responsedata.put(TagWords.SUCCESS,false);
			responsedata.put(TagWords.MSG,WarnWrods.ERROR_PARAMS);
		}else{
			GoodsTypeT bean=this.goodsTypeTService.findByPK(GoodsTypeT.class, rq.getGoodstypeid());
			if(bean!=null){
				responsedata.put(TagWords.BEAN,bean);
				responsedata.put(TagWords.SUCCESS,true);
				responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
			}else{
				responsedata.put(TagWords.SUCCESS,false);
				responsedata.put(TagWords.MSG,TagWords.INVALIDDATA);
			}
		}
		return Response.success(responsedata);
	}


	/**
	 *
	 * @api {post} /mall/goodstype/update 更新一条商品类型
	 * @apiName 更新一条的商品类型
	 * @apiGroup  GoodsService
	 * @apiDescription 更新一条商品类型
	 * @apiVersion 0.0.1
	 * @apiParam {String} name 商品类型名称
	 * @apiParam {String} goodsParameter 商品类型参数
	 * @apiParam {String} status 记录状态 启用或者禁用
	 *
	 * @apiSuccess {String} sucflag true标记
	 * @apiSuccess {String} msg  提示信息
	 * @apiSuccess {GoodsTypeT} bean 数据对象
	 *
	 * @apiUse operationError
	 * @apiUse operationSuccess
	 */
	@RequestMapping(value="/update",method= RequestMethod.POST)
	@Override
	public ResponseEntity update(@RequestBody GoodsTypeT rq) {
		if(StringUtils.isBlank(rq.getGoodstypeid())){
			responsedata.put(TagWords.SUCCESS,false);
			responsedata.put(TagWords.MSG,WarnWrods.ERROR_PARAMS);
		}else{
			GoodsTypeT bean=this.goodsTypeTService.findByPK(GoodsTypeT.class, rq.getGoodstypeid());
			if(bean!=null){
				bean.setName(StringUtils.trim(rq.getName()));
				bean.setGoodsParameter(rq.getGoodsParameter());
				bean.setStatus(rq.getStatus());
				bean.setCreatorid(BaseTools.getAdminCreateId());
				bean.setUpdatetime(DateTools.getSystemTimeBi());
				bean.setVersiont(bean.getVersiont()+1);
				this.goodsTypeTService.update(bean);
				responsedata.put(TagWords.SUCCESS,true);
				responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
			}else{
				responsedata.put(TagWords.SUCCESS,false);
				responsedata.put(TagWords.MSG,TagWords.INVALIDDATA);
			}
		}
		return Response.success(responsedata);
	}


	/**
	 *
	 * @api {post} /mall/goodstype/del 删除商品类型
	 * @apiName 删除商品类型
	 * @apiGroup  GoodsService
	 * @apiDescription 后台管理员删除商品类型
	 * @apiVersion 0.0.1
	 * @apiParam {String} goodstypeid 商品类型id
	 *
	 * @apiSuccess {String} sucflag true标记
	 * @apiSuccess {String} msg  提示信息
	 *
	 * @apiUse operationError
	 * @apiUse operationSuccess
	 */
	@RequestMapping(value="/del",method= RequestMethod.POST)
	@Override
	public ResponseEntity del(@RequestBody GoodsTypeT rq) {
		if(StringUtils.isBlank(rq.getGoodstypeid())){
			responsedata.put(TagWords.SUCCESS,false);
			responsedata.put(TagWords.MSG,WarnWrods.ERROR_PARAMS);
		}else{
			String ids[]=StringUtils.split(rq.getGoodstypeid(), TagWords.SPLITDOT);
			for(String s:ids){
				GoodsTypeT bean=this.goodsTypeTService.findByPK(GoodsTypeT.class, s);
				if(bean!=null){
					bean.setStatus(BaseEnums.DataUsingState.DEL.getState());
					bean.setUpdatetime(DateTools.getSystemTimeBi());
					this.goodsTypeTService.update(bean);
				}
			}
			responsedata.put(TagWords.SUCCESS,true);
			responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
		}
		return Response.success(responsedata);
	}



	/**
	 *
	 * @api {post} /mall/goodstype/findAll 查询所有商品类型
	 * @apiName 查询所有的商品类型
	 * @apiGroup  GoodsService
	 * @apiDescription 查询所有商品类型
	 * @apiVersion 0.0.1
	 * @apiSuccess {String} sucflag true标记
	 * @apiSuccess {String} msg  提示信息
	 * @apiSuccess {List} beanlist 数据集合
	 *
	 * @apiUse operationError
	 * @apiUse operationSuccess
	 */
	@RequestMapping(value="/findAll",method= RequestMethod.POST)
	@Override
	public ResponseEntity findAll() {
		Criterion criterion=Restrictions.eq(TagWords.STATUS, BaseEnums.DataUsingState.USING.getState());
		List<GoodsTypeT>list=this.goodsTypeTService.findByCriteria(GoodsTypeT.class, criterion,Order.desc(TagWords.UPDATETIME));
		responsedata.put(TagWords.BEANLIST,list);
		responsedata.put(TagWords.SUCCESS, true);
		responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
		return Response.success(responsedata);
	}



	/**
	 *
	 * @api {post} /mall/goodstype/findByPage 查询商品类型列表
	 * @apiName  查询商品类型列表
	 * @apiGroup  GoodsService
	 * @apiDescription 后台管理员查询商品类型列表 1，区分是否有排序，是否有搜索，然后进行相应的业务方法调用
	 * @apiVersion 0.0.1
	 * @apiParam {SearchModel} searchModel 搜索模型对象
	 * @apiParam {String} search 快捷搜索对象
	 * @apiParam {String} ordervalue 排序对象
	 * @apiParam {String} ordertype 排序方式
	 *
	 * @apiSuccess {String} sucflag true标记
	 * @apiSuccess {String} msg  提示信息
	 * @apiSuccess {map} data 搜索结果对象
	 *
	 * @apiUse operationError
	 * @apiUse operationSuccess
	 */
	@RequestMapping(value="/findByPage",method= RequestMethod.POST)
	@Override
	public ResponseEntity findByPage(SearchModel searchModel, @RequestParam("search[value]") String search, @RequestParam("order[0][column]") String ordervalue, @RequestParam("order[0][dir]") String ordertype) {
		//检查是否有自定义搜索内容 1标示进行自定义搜索逻辑
		findCustomSearch(searchModel,search,ordervalue,ordertype);
		return Response.success(responsedata);
	}



	@Override
	public void findCustomSearch(SearchModel searchModel, String search, String ordervalue, String ordertype) {
		//如果有自定义搜索
		if(searchModel.getIsCustomSearch()==1){
			int currentPage=searchModel.getCalCurrentPage();
			int lineSize = searchModel.getLength();
			Criterion criterion=Restrictions.ne(TagWords.STATUS, BaseEnums.DataUsingState.DEL.getState());
			if(StringUtils.isNotBlank(searchModel.getName())){
				criterion=Restrictions.and(criterion).add(Restrictions.like("name",searchModel.getName(), MatchMode.ANYWHERE));
			}
			if(StringUtils.isNotBlank(search)){
				criterion=Restrictions.and(criterion).add(Restrictions.like("name",search,MatchMode.ANYWHERE));
			}
			int records=this.goodsTypeTService.count(GoodsTypeT.class,criterion).intValue();
			searchModel.setRecordsTotal(records);
			searchModel.setRecordsFiltered(records);
			responsedata.put(TagWords.RECORDSTOTAL,records);
			responsedata.put(TagWords.RECORDSFILTERED,records);
			List<GoodsTypeT> list = this.goodsTypeTService.findByCriteriaByPage(
					GoodsTypeT.class, criterion,Order.desc(TagWords.UPDATETIME), currentPage,
					lineSize);
			this.processPageList(list);
		}else{
			if(StringUtils.isNotBlank(search)){
				filterSearch(searchModel,search);
			}else{
				findDefaultAll(searchModel,ordervalue,ordertype);
			}
		}
	}

	@Override
	public void filterSearch(SearchModel searchModel, String search) {
		int currentPage=searchModel.getCalCurrentPage();
		int lineSize = searchModel.getLength();
		Criterion criterion;
		Criterion criterion1=Restrictions.ne(TagWords.STATUS, BaseEnums.DataUsingState.DEL.getState());
		Criterion criterion2=Restrictions.like("name",search,MatchMode.ANYWHERE);
		criterion=Restrictions.and(criterion1).add(criterion2);
		int records=this.goodsTypeTService.count(GoodsTypeT.class,criterion).intValue();
		searchModel.setRecordsTotal(records);
		searchModel.setRecordsFiltered(records);
		responsedata.put(TagWords.RECORDSTOTAL,records);
		responsedata.put(TagWords.RECORDSFILTERED,records);
		List<GoodsTypeT> list = this.goodsTypeTService.findByCriteriaByPage(
				GoodsTypeT.class, criterion,Order.desc(TagWords.UPDATETIME), currentPage,
				lineSize);
		this.processPageList(list);
	}

	@Override
	public void findDefaultAll(SearchModel searchModel, String ordervalue, String ordertype) {
		int currentPage=searchModel.getCalCurrentPage();
		int lineSize = searchModel.getLength();
		Criterion criterion=Restrictions.ne(TagWords.STATUS, BaseEnums.DataUsingState.DEL.getState());
		int records=this.goodsTypeTService.count(GoodsTypeT.class,criterion).intValue();
		searchModel.setRecordsFiltered(records);
		searchModel.setRecordsTotal(records);
		responsedata.put(TagWords.RECORDSTOTAL,records);
		responsedata.put(TagWords.RECORDSFILTERED,records);
		Order order=BaseTools.orderByParam(TagWords.UPDATETIME,ordervalue,ordertype);
		List<GoodsTypeT> list = this.goodsTypeTService.findByCriteriaByPage(
				GoodsTypeT.class, criterion,order, currentPage,
				lineSize);
		this.processPageList(list);
	}

	@Override
	public void processPageList(List<GoodsTypeT> list) {
		List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
		for (GoodsTypeT bean:list) {
			Map<String, Object> cellMap = new HashMap<String, Object>();
			cellMap.put("id", bean.getGoodstypeid());
			cellMap.put("name", bean.getName());
			cellMap.put("status", BaseEnums.DataUsingState.getName(bean.getStatus()));
			cellMap.put("updatetime", DateTools.formateLongDateToString(bean.getUpdatetime().longValue()));
			cellMap.put("version", bean.getVersiont());
			data.add(cellMap);
		}
		responsedata.put(TagWords.DATA, data);
	}



	@RequestMapping(value="/findGoodsTypeByPage",method= RequestMethod.POST)
	public ResponseEntity findGoodsTypeByPage(SearchModel searchModel, @RequestParam("search[value]") String search, @RequestParam("order[0][column]") String ordervalue, @RequestParam("order[0][dir]") String ordertype) {
		//检查是否有自定义搜索内容 1标示进行自定义搜索逻辑
		findGoodsTypeCustomSearch(searchModel,search,ordervalue,ordertype);
		return Response.success(responsedata);
	}

	private void findGoodsTypeCustomSearch(SearchModel searchModel, String search, String ordervalue, String ordertype) {
		//如果有自定义搜索
		if(searchModel.getIsCustomSearch()==1){
			int currentPage=searchModel.getCalCurrentPage();
			int lineSize = searchModel.getLength();
			Criterion criterion=Restrictions.and(Restrictions.ne(TagWords.STATUS, BaseEnums.DataUsingState.DEL.getState()));
			if(StringUtils.isNotBlank(search)){
				criterion=Restrictions.and(criterion).add(Restrictions.like("name",search,MatchMode.ANYWHERE));
			}
			int records=this.goodsTypeTService.count(GoodsTypeT.class,criterion).intValue();
			searchModel.setRecordsTotal(records);
			searchModel.setRecordsFiltered(records);
			responsedata.put(TagWords.RECORDSTOTAL,records);
			responsedata.put(TagWords.RECORDSFILTERED,records);
			List<GoodsTypeT> list = this.goodsTypeTService.findByCriteriaByPage(
					GoodsTypeT.class, criterion, Order.desc(TagWords.UPDATETIME), currentPage,
					lineSize);
			this.processGoodsTypePageList(list);
		}else{
			if(StringUtils.isNotBlank(search)){
				filterGoodsTypeSearch(searchModel,search);
			}else{
				findGoodsTypeDefaultAll(searchModel,ordervalue,ordertype);
			}
		}
	}

	private void filterGoodsTypeSearch(SearchModel searchModel, String search) {
		int currentPage=searchModel.getCalCurrentPage();
		int lineSize = searchModel.getLength();
		Criterion criterion=Restrictions.ne(TagWords.STATUS, BaseEnums.DataUsingState.DEL.getState());
		if(StringUtils.isNotBlank(search)){
			criterion=Restrictions.and(criterion).add(Restrictions.like("name",search,MatchMode.ANYWHERE));
		}
		int records=this.goodsTypeTService.count(GoodsTypeT.class,criterion).intValue();
		searchModel.setRecordsTotal(records);
		searchModel.setRecordsFiltered(records);
		responsedata.put(TagWords.RECORDSTOTAL,records);
		responsedata.put(TagWords.RECORDSFILTERED,records);
		List<GoodsTypeT> list = this.goodsTypeTService.findByCriteriaByPage(
				GoodsTypeT.class, criterion,Order.desc(TagWords.UPDATETIME), currentPage,
				lineSize);
		this.processGoodsTypePageList(list);
	}


	private void findGoodsTypeDefaultAll(SearchModel searchModel, String ordervalue, String ordertype) {
		int currentPage=searchModel.getCalCurrentPage();
		int lineSize = searchModel.getLength();
		Criterion criterion=Restrictions.ne(TagWords.STATUS, BaseEnums.DataUsingState.DEL.getState());
		int records=this.goodsTypeTService.count(GoodsTypeT.class,criterion).intValue();
		searchModel.setRecordsFiltered(records);
		searchModel.setRecordsTotal(records);
		responsedata.put(TagWords.RECORDSTOTAL,records);
		responsedata.put(TagWords.RECORDSFILTERED,records);
		Order order=BaseTools.orderByParam(TagWords.UPDATETIME,ordervalue,ordertype);
		List<GoodsTypeT> list = this.goodsTypeTService.findByCriteriaByPage(
				GoodsTypeT.class, criterion,order, currentPage,
				lineSize);
		this.processGoodsTypePageList(list);
	}


	private void processGoodsTypePageList(List<GoodsTypeT> list) {
		List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
		for (GoodsTypeT bean:list) {
			Map<String, Object> cellMap = new HashMap<String, Object>();
			cellMap.put("id", bean.getGoodstypeid());
			cellMap.put("name", bean.getName());
			cellMap.put("status", BaseEnums.DataUsingState.getName(bean.getStatus()));
			cellMap.put("updatetime", DateTools.formateLongDateToString(bean.getUpdatetime().longValue()));
			data.add(cellMap);
		}
		responsedata.put(TagWords.DATA, data);
	}


	/**
	 * 检查是否有重名商品类型名称
	 * @return
	 */
	@Override
	public boolean checkDuplicates(String str) {
		if(StringUtils.isBlank(str)){
			return false;
		}
		Criterion criterion=Restrictions.eq("name",str);
		GoodsTypeT goodsTypeT=this.goodsTypeTService.findOneByCriteria(GoodsTypeT.class, criterion);
		if(goodsTypeT!=null){
			return false;
		}
		return true;
	}

}
